using System;
using System.Collections;
using UnityEngine;

/// <summary>
/// 处理游戏对象的旋转，并提供一个“旋转锁”机制。
/// 这是一个纯逻辑组件，需要被添加到 Entity 中，并由 Entity 的 Update 驱动。
/// </summary>
[Serializable]
public class RotationComponent : BaseComponent
{
    [Header("1秒可以旋转多少度（其实就是旋转速度啦）")]
    public float rotationSpeed = 720f;
    private object currentLocker = null;
    private Quaternion targetRotation;
    public bool hasTarget = false;

    public override void OnAwake()
    {
        // 初始化时，将当前旋转设为目标旋转，避免初始时突然转向
        if (entity != null)
        {
            targetRotation = entity.transform.rotation;
        }
    }

    public override void OnUpdate()
    {
        if (entity == null || !hasTarget)
        {
            return;
        }

        // 平滑地转向目标旋转
        if (entity.transform.rotation != targetRotation)
        {
            
            entity.transform.rotation = Quaternion.RotateTowards(entity.transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
            if (entity.transform.rotation == targetRotation) {
                targetRotation = entity.transform.rotation;
            }
        }
    }

    /// <summary>
    /// 设置旋转速度
    /// </summary>
    public void SetRotationSpeed(float speed)
    {
        this.rotationSpeed = speed;
    }

    /// <summary>
    /// 尝试锁定并设置旋转方向。
    /// </summary>
    /// <param name="locker">请求锁定的对象，作为唯一的“钥匙”。</param>
    /// <param name="direction">新的目标方向。</param>
    /// <returns>如果成功获取或更新了锁，则返回 true。</returns>
    public bool TryLockAndRotateToDirection(object locker, Vector3 direction)
    {
        if (direction == Vector3.zero) return false;

        if (currentLocker == null || currentLocker == locker)
        {
            currentLocker = locker;
            targetRotation = Quaternion.LookRotation(direction);
            hasTarget = true;
            return true;
        }
        else if (currentLocker != locker)
        {
            Debug.LogError("当前已经上锁，无法被 " + locker.ToString() + " 锁定");
        }
        return false;
    }

    /// <summary>
    /// 释放由特定对象持有的锁。
    /// </summary>
    /// <param name="locker">之前用于锁定的那个“钥匙”对象。</param>
    public void ReleaseLock(object locker)
    {
        if (currentLocker == locker)
        {
            currentLocker = null;
            hasTarget = false; // 锁被释放，不再自动旋转
        }
    }

    /// <summary>
    /// 发出一个标准的、非锁定的旋转请求。如果组件当前被锁定，此请求将被忽略。
    /// </summary>
    /// <param name="direction">新的目标方向。</param>
    public void RotateToDirection(Vector3 direction)
    {
        if (direction == Vector3.zero) return;

        if (currentLocker == null)
        {
            targetRotation = Quaternion.LookRotation(direction);
            hasTarget = true;
        }
    }

    // //旋转到某个方向，只旋转一次
    // IEnumerator RotateToDirectionOne(Vector3 direction)
    // {
    //     if (direction == Vector3.zero) yield break;
    //     Quaternion targetRotation = Quaternion.LookRotation(direction);
    //     while (entity.transform.rotation != targetRotation)
    //     {
    //         entity.transform.rotation = Quaternion.RotateTowards(entity.transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    //         yield return null;
    //     }
    //     // entity.transform.rotation = Quaternion.RotateTowards(entity.transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    // }
}
